﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using FinalPointSet.Model.Constant;
using FinalPointSet.Model.Constant.Message;
using FinalPointSet.Model.Constants;
using FinalPointSet.Model.Manager;
using FinalPointSet.Model.Model;
using FinalPointSet.Model.Model.FieldMapping;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using static FinalPointSet.Model.Manager.MessageManager;

namespace FinalPointSet.ViewModel.Page.FieldMapping
{
    public partial class MappingRuleSettingPageViewModel : ObservableObject, IDisposable
    {

        #region 属性
        /// <summary>
        /// 数据源集合
        /// </summary>
        private List<DataSource> dataSources = new();
        /// <summary>
        /// 数据源映射规则
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<DataSourceMapping> _DataSourceMappings = new();
        /// <summary>
        /// 来源数据源
        /// </summary>
        [ObservableProperty]
        private DataSource _SourceDataSource = new();
        /// <summary>
        /// 目标数据源
        /// </summary>
        [ObservableProperty]
        private DataSource _TargetDataSource = new();
        /// <summary>
        /// 数据源映射规则
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<FieldMappingKeyValuePair> _FieldMappingKeyValuePairs = new();
        /// <summary>
        /// 来源数据源Logo可见性
        /// </summary>
        [ObservableProperty]
        private Visibility _SourceDataSouceLogoVisibility = Visibility.Hidden;
        /// <summary>
        /// 目标数据源Logo可见性
        /// </summary>
        [ObservableProperty]
        private Visibility _TargetDataSouceLogoVisibility = Visibility.Hidden;
        /// <summary>
        /// 当前选中的规则
        /// </summary>
        [ObservableProperty]
        private FieldMappingKeyValuePair _SelectedItem = new();
        #endregion
        public MappingRuleSettingPageViewModel()
        {
            //初始化数据
            InitResource();

            //监听数据源集合资源变化
            AssetManager.Instance.OnJsonObjectListUpdate += OnDataSourceUpdate;
        }

        private void OnDataSourceUpdate(Type type)
        {
            if (type == typeof(DataSource))
                InitResource();
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitResource()
        {
            //填充数据到目标规则集合
            DataSourceMappings = new(DataSourceMappingHelper.GetDataSourceMappings());
        }

        /// <summary>
        /// 数据源映射规则树视图选择变更
        /// </summary>
        /// <param name="selectedItem">选中的节点</param>
        [RelayCommand]
        private void DataSourceTreeViewSelectedItemChange(object selectedItem)
        {
            if (selectedItem == null) return;
            Debug.Print(selectedItem.ToString());
            //一级节点
            if (selectedItem is DataSourceMapping mapping)
            {
                SourceDataSouceLogoVisibility = Visibility.Visible;
                TargetDataSouceLogoVisibility = Visibility.Hidden;
                SourceDataSource = mapping.SourceDataSource;
                TargetDataSource = new();
                FieldMappingKeyValuePairs.Clear();
            }
            //二级节点
            else if (selectedItem is DataSourceMappingRule mappingRule)
            {
                SourceDataSouceLogoVisibility = Visibility.Visible;
                TargetDataSouceLogoVisibility = Visibility.Visible;
                SourceDataSource = mappingRule.SourceDataSource;
                TargetDataSource = mappingRule.TargetDataSource;
                FieldMappingKeyValuePairs = new(mappingRule.FieldTypeMapping);
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        [RelayCommand]
        private void Save()
        {
            if (DataSourceMappings == null) return;
            //保存数据
            AssetManager.Instance.UpdateJsonObjectList(DataSourceMappings.ToList());
            AssetManager.Instance.SaveJsonObjectList<DataSourceMapping>(new DataSourceMappingConverter());
            Broadcast(MsgType.GrowMsg, new GrowMsgArg(WindowType.MainView, GrowlMsgType.Success, $"保存数据源映射规则成功"));
        }

        private DataSourceMappingRule? FindDataSouceMappingRule()
        {
            return DataSourceMappingHelper.FindDataSourceMappingRule([.. DataSourceMappings], SourceDataSource, TargetDataSource);
        }


        public void Dispose()
        {
            AssetManager.Instance.OnJsonObjectListUpdate -= OnDataSourceUpdate;
        }
    }
}
